package controller;

import entity.FileSystem;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class FileAction {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private Redisson redisson;


    @RequestMapping("upload")
    public @ResponseBody FileSystem upload(MultipartHttpServletRequest request) throws Exception {

        // request 强化版本
        FileSystem fileSystem = new FileSystem();

        // 定义商品 ID
        String productKey = "UploadVideos";
        // 获取锁  底层集成了多个操作
        RLock rLock = redisson.getLock(productKey);
        // 上锁
        rLock.lock(30, TimeUnit.SECONDS);

        try {
            // 1......  将文件上传到自己的  web服务器

            // 获取上传的文件对象
            MultipartFile file = request.getFile("fname");

            String oldFileName = file.getOriginalFilename();

            String hou = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);

            // 生成 UUID 全新文件名
            String newFileName = UUID.randomUUID() + "." + hou;

            // 创建 web 服务器 保存的文件目录
            File toSaveFile = new File("D:/upload/" + newFileName);

            file.transferTo(toSaveFile);

            // 路径转换为文件
            String newFilePath = toSaveFile.getAbsolutePath();


            // 2......  文件从 web 服务器上传到FastDFS
            ClientGlobal.initByProperties("config/fastdfs-client.properties");
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageServer storageServer = null;
            StorageClient1 client = new StorageClient1(trackerServer, storageServer);

            NameValuePair[] list = new NameValuePair[1];
            list[0] = new NameValuePair("fileName", oldFileName);
            String fileId = client.upload_appender_file1(newFilePath, hou, list);
            trackerServer.close();

            // 封装fileSystem 对象
            fileSystem.setFileId(fileId);
            fileSystem.setFileName(newFileName);
            fileSystem.setFilePath(fileId);  // 已经上传到 FastDFS 上， 通过fileID l来访问图片

            System.out.println("上传成功!!!");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }


        return fileSystem;


    }

    @Bean
    public Redisson redisson(){
        Config config = new Config();
        // 使用单个 redis 服务器
        config.useSingleServer().setAddress("redis://192.168.79.128:6379").setDatabase(0);

        return (Redisson) Redisson.create(config);

    }

}
